package com.heima.common.excpetionCatch;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.common.excpetion.CustomException;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@Configuration
@RestControllerAdvice
public class ExceptionCatch {
    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionCatch(Exception exception) {
        exception.printStackTrace();
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "您的网络异常，请稍后重试");
    }

    @ExceptionHandler(CustomException.class)
    public ResponseResult CustomExceptionCatch(CustomException exception) {
        exception.printStackTrace();
        //获取自定义的异常信息
        AppHttpCodeEnum appHttpCodeEnum = exception.getAppHttpCodeEnum();
        //返回给前端
        return ResponseResult.errorResult(appHttpCodeEnum);
    }
}
